package com.redrose.videodemo.photo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.redrose.videodemo.R;
import com.redrose.videodemo.adapter.ItemImageAdapter;
import com.redrose.videodemo.base.BaseActivity;
import com.redrose.videodemo.bean.Photo;
import com.redrose.videodemo.bean.Time;
import com.redrose.videodemo.utils.LogUtils;
import com.redrose.videodemo.view.NoScrollGridView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.litepal.crud.DataSupport;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Desc: 图库预览
 * Author: RedRose
 * Date: 2019/3/31
 * Email: yinsxi@163.com
 */
@ContentView(R.layout.activity_image)
public class ImageActivity extends BaseActivity {
    @ViewInject(R.id.image_recycle)
    private RecyclerView mImageRecycleView;
    //    private List<Photo> mPhotoList;
    private List<Time> mTimeList;
    private ImageAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Bundle bundleExtra = intent.getBundleExtra("data");
        if (bundleExtra != null) {
//            mPhotoList = (List<Photo>) bundleExtra.getSerializable("photoList");
        }
        initRecycle(mImageRecycleView);
        queryPhotos();
        mAdapter = new ImageAdapter(mContext, R.layout.image_item_layout, mTimeList);
        mImageRecycleView.setAdapter(mAdapter);
    }

    private void queryPhotos() {
        mTimeList = DataSupport.order("classId desc").find(Time.class, false);
//        mAdapter.notifyDataSetChanged();
        if(mTimeList.size() == 0){

        }

    }

    private void initRecycle(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
//        recyclerView.setAdapter(recycleAdapter);
        //设置分隔线
//        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    private class ImageAdapter extends CommonAdapter<Time> {
        private ImageAdapter(Context context, int layoutId, List<Time> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, Time time, int position) {
            TextView timeText = holder.getView(R.id.image_time);
            timeText.setText(String.format("%d/%d/%d", time.getYear(), time.getMonth(), time.getDay()));
            GridView gridview = holder.getView(R.id.image_gridview);
            List<Photo> mPhotoList = time.getPhotoList();
            ItemImageAdapter adapter = new ItemImageAdapter(mContext, R.layout.single_image_layout, mPhotoList);
            gridview.setAdapter(adapter);
            /** 解决 griview.getNumCouns 返回-1 的问题，需要等待加载完毕才能去获取num */
//            gridview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                @Override
//                public void onGlobalLayout() {
//                    gridview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//
//                }
//
//            });

        }
    }
}
